{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "9bc9c89e-3174-40bd-9885-e12339b11691",
   "metadata": {},
   "source": [
    "# Applications"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dfd47055-fe5e-4d2a-83e2-27ae8afdbdfc",
   "metadata": {
    "height": 47
   },
   "outputs": [],
   "source": [
    "def json_print(data):\n",
    "    print(json.dumps(data, indent=2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8f840bfe-d592-414e-90ce-a04dfc3cd87e",
   "metadata": {
    "height": 318
   },
   "outputs": [],
   "source": [
    "import weaviate, os, json\n",
    "import openai\n",
    "from dotenv import load_dotenv, find_dotenv\n",
    "_ = load_dotenv(find_dotenv()) # read local .env file\n",
    "\n",
    "auth_config = weaviate.auth.AuthApiKey(api_key=os.getenv(\"WEAVIATE_API_KEY\"))\n",
    "\n",
    "client = weaviate.Client(\n",
    "    url=os.getenv(\"WEAVIATE_API_URL\"),\n",
    "    auth_client_secret=auth_config,\n",
    "    additional_headers={\n",
    "        \"X-Cohere-Api-Key\": os.getenv(\"COHERE_API_KEY\"),\n",
    "        \"X-Cohere-BaseURL\": os.getenv(\"CO_API_URL\")\n",
    "    }\n",
    ")\n",
    "\n",
    "client.is_ready() #check if True"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7226230a-0070-4083-8d21-f2b0a7297ba4",
   "metadata": {},
   "source": [
    "### 2. How many vectors are stored in this database"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c25dd1d9-004e-47bf-9e4d-dc0ed837ccac",
   "metadata": {
    "height": 46
   },
   "outputs": [],
   "source": [
    "print(json.dumps(client.query.aggregate(\"Wikipedia\").with_meta_count().do(), indent=2))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e25de586-0e84-4658-9acf-4d39a09755bb",
   "metadata": {},
   "source": [
    "### 3. Perform search over them to find concepts you are interested in!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c2580bd6-6d7a-40e8-b301-1a656bc3b95c",
   "metadata": {
    "height": 165
   },
   "outputs": [],
   "source": [
    "response = (client.query\n",
    "            .get(\"Wikipedia\",['text','title','url','views','lang'])\n",
    "            .with_near_text({\"concepts\": \"Vacation spots in california\"})\n",
    "            .with_limit(5)\n",
    "            .do()\n",
    "           )\n",
    "\n",
    "json_print(response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1b27c2ba-c8fe-40e1-8457-5c7217bf28e2",
   "metadata": {
    "height": 250
   },
   "outputs": [],
   "source": [
    "response = (client.query\n",
    "            .get(\"Wikipedia\",['text','title','url','views','lang'])\n",
    "            .with_near_text({\"concepts\": \"Vacation spots in california\"})\n",
    "            .with_where({\n",
    "                \"path\" : ['lang'],\n",
    "                \"operator\" : \"Equal\",\n",
    "                \"valueString\":'en'\n",
    "            })\n",
    "            .with_limit(3)\n",
    "            .do()\n",
    "           )\n",
    "\n",
    "json_print(response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "efc9d5cc-66e8-4650-8de0-331e899fb123",
   "metadata": {
    "height": 250
   },
   "outputs": [],
   "source": [
    "response = (client.query\n",
    "            .get(\"Wikipedia\",['text','title','url','views','lang'])\n",
    "            .with_near_text({\"concepts\": \"Miejsca na wakacje w Kalifornii\"})\n",
    "            .with_where({\n",
    "                \"path\" : ['lang'],\n",
    "                \"operator\" : \"Equal\",\n",
    "                \"valueString\":'en'\n",
    "            })\n",
    "            .with_limit(3)\n",
    "            .do()\n",
    "           )\n",
    "\n",
    "json_print(response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1290303f-6833-4737-acf3-8ded3144aaaa",
   "metadata": {
    "height": 250
   },
   "outputs": [],
   "source": [
    "response = (client.query\n",
    "            .get(\"Wikipedia\",['text','title','url','views','lang'])\n",
    "            .with_near_text({\"concepts\": \"أماكن العطلات في كاليفورنيا\"})\n",
    "            .with_where({\n",
    "                \"path\" : ['lang'],\n",
    "                \"operator\" : \"Equal\",\n",
    "                \"valueString\":'en'\n",
    "            })\n",
    "            .with_limit(3)\n",
    "            .do()\n",
    "           )\n",
    "\n",
    "json_print(response)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1756b652-257d-4cca-95e3-f40870884113",
   "metadata": {},
   "source": [
    "## Retrieval Augmented Generation"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "14074da1-1038-4fad-8241-1ba8db8200a2",
   "metadata": {},
   "source": [
    "### Single Prompt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4d40ce8d-a785-4f47-9497-5cc102ec4349",
   "metadata": {
    "height": 233
   },
   "outputs": [],
   "source": [
    "prompt = \"Write me a facebook ad about {title} using information inside {text}\"\n",
    "result = (\n",
    "  client.query\n",
    "  .get(\"Wikipedia\", [\"title\",\"text\"])\n",
    "  .with_generate(single_prompt=prompt)\n",
    "  .with_near_text({\n",
    "    \"concepts\": [\"Vacation spots in california\"]\n",
    "  })\n",
    "  .with_limit(3)\n",
    ").do()\n",
    "\n",
    "json_print(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2b3a82f0-89e8-4ddb-8973-1bc84d3e10ee",
   "metadata": {},
   "source": [
    "### Group Task"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "06097742-ed4b-4a6f-94a3-3c85d2bbba0c",
   "metadata": {
    "height": 267
   },
   "outputs": [],
   "source": [
    "generate_prompt = \"Summarize what these posts are about in two paragraphs.\"\n",
    "\n",
    "result = (\n",
    "  client.query\n",
    "  .get(\"Wikipedia\", [\"title\",\"text\"])\n",
    "  .with_generate(grouped_task=generate_prompt) # Pass in all objects at once\n",
    "  .with_near_text({\n",
    "    \"concepts\": [\"Vacation spots in california\"]\n",
    "  })\n",
    "  .with_limit(3)\n",
    ").do()\n",
    "\n",
    "json_print(result)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3529614d-8354-43be-87de-46bd9728781e",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3bc5b056-8d34-4fe3-8cf7-467050a3d1bb",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "20f4c1c1-7406-463e-a8af-164c82e9a31e",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "85c1e824-98bf-4e80-b460-1e511ea63595",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a45dc13f-a183-48ed-8c7c-e62160103f9c",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0ffc52d9-3a8d-4104-98ef-b520aaba2494",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "60ce5c96-41df-4a98-a647-5895074c451e",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "294ac8ca-8a18-4641-8859-ce12b6abdcd1",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fbd90f59-e48f-4a2a-9528-c7cf6ae693ff",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4065f442-6b70-44eb-80ea-87d01909a075",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fc0bfd03-aa29-47e8-8d8b-dc79a8b7e48e",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "98248c4e-080c-457e-82f9-a0305d9649ae",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2bf8a650-4afb-461d-a8ce-cfbc333ba778",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bc31c387-a0d6-4967-8ef0-84c883f31c3a",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c4933328-d1bc-421f-a54f-f63294450177",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "564a167e-83b9-4023-aa63-ddfd9a2aab5e",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0d26d6a6-da0c-4f6e-9a38-fc9770dc6a36",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "416d1878-295d-479a-acfc-dfd6bf418f7d",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2d5e6329-8a93-4a53-9d4e-d1fa89610ff9",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0543e1f7-3005-4347-93ad-d072480f8caa",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6f40fb43-4d53-4096-b229-9f1ca2fd1bb3",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "514f4349-0102-442b-ab6a-0a045e5522d4",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "725b34bf-5853-4f39-890b-4da9f3852343",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1cbb7c9f-8c11-4809-b7b0-f85a4a922115",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6a0bf6a0-944b-4fbb-aec1-d01cd587b4dc",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "62a5940d-cfea-4ae4-be24-399a1e8f8661",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f15cec08-fb7c-494e-a4dc-c26c9ad2bf10",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9a16dcb6-f109-42fc-b42f-06a76f32e7bd",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8998c471-91d7-4578-9df1-a5661992121f",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e4cbd8a7-0fac-47ae-bbf6-b4306b9e008e",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7d9bd003-2dd7-431e-9d58-a38c090aa72b",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e2135808-4fb6-4f47-bac4-5b30f80c8d91",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "91b11892-bcdc-434a-92da-f5633e865035",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5b37bcd4-c948-4b78-989c-04a50844381b",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "34fffd7f-58fc-4f23-b009-b8d4b0622844",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "79f2935e-f4c7-48e7-b9f9-02861f4db3f5",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.19"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
